﻿
using System;
using System.Net.Mail;


//
namespace Wozuji.Common.Email
{

    /// <summary>
    /// CEmail 邮件类
    /// </summary>

    public class CEmail
    {

        #region Variables

        private string m_strSenderMail;   //发送者的Email
        private string m_strRecipientMail;  //接收者的Email
        private string m_strCC;     //抄送者的Email
        private string m_strBCC;    //秘密抄送者的Email
        private string m_strMailSubject;  //Email的Subject
        private string m_strMailBody;   //Email的正文
        private string m_strMailServerName;  //SMTP Server的地址 Global.g_SMTPServer

        private string m_strarrayMailAttachmentList;     //Email的附件数组
        private System.Text.Encoding m_encodingMailBodyEncoding;  //Email正文的编码方式
        private bool m_lMailAuthentication; //是否使用smtp身份验证
        private string m_lMailUsername;
        private string m_lMailUserPassword;
        public string errorMessage;

        public bool SendSucess = true;


        #endregion

        #region Property
        /// <summary>
        /// sender's email address
        /// </summary>

        public string SenderMail
        {
            get
            {
                return m_strSenderMail;
            }
            set
            {
                m_strSenderMail = value;
            }
        }
        /// <summary>
        /// 抄送者的email address
        /// </summary>
        public string CC
        {
            get
            {
                return m_strCC;
            }
            set
            {
                m_strCC = value;
            }
        }
        /// <summary>
        /// recipient email address
        /// </summary>
        public string RecipientMail
        {
            get
            {
                return m_strRecipientMail;
            }
            set
            {
                m_strRecipientMail = value;
            }
        }

        /// <summary>
        /// 密件抄送者的email address
        /// </summary>
        public string BCC
        {
            get
            {
                return m_strBCC;
            }
            set
            {
                m_strBCC = value;
            }
        }

        /// <summary>
        /// mail's subject
        /// </summary>
        public string MailSubject
        {
            get
            {
                return m_strMailSubject;
            }
            set
            {
                m_strMailSubject = value;
            }
        }

        /// <summary>
        /// mail's encoding type
        /// </summary>
        public System.Text.Encoding MailBodyEncoding
        {
            get
            {
                return m_encodingMailBodyEncoding;
            }
            set
            {
                m_encodingMailBodyEncoding = value;
            }
        }


        /// <summary>
        /// mail's body
        /// </summary>
        public string MailBody
        {
            get
            {
                return m_strMailBody;
            }
            set
            {
                m_strMailBody = value;
            }
        }

        /// <summary>
        /// mail's attachment list
        /// attachment's full path & name
        /// </summary>
        public string MailAttachmentList
        {
            get
            {
                return m_strarrayMailAttachmentList;
            }
            set
            {
                m_strarrayMailAttachmentList = value;
            }
        }

        ///<summary>
        ///mail authentication flag
        ///</summary>
        public bool MailAuthentication
        {
            get
            {
                return m_lMailAuthentication;
            }
            set
            {
                m_lMailAuthentication = value;
            }
        }
        /// <summary>
        /// mail username
        /// </summary>
        public string MailUsername
        {
            get
            {
                return m_lMailUsername;
            }
            set
            {
                m_lMailUsername = value;
            }
        }
        ///<summary>
        ///mail user password
        ///</summary> 
        public string MailUserPassword
        {
            get
            {
                return m_lMailUserPassword;
            }
            set
            {
                m_lMailUserPassword = value;
            }
        }
        /// Smtp Server's IP
        /// </summary>
        public string MailServerName
        {
            get
            {
                return m_strMailServerName;
            }
            set
            {
                m_strMailServerName = value;
            }
        }


        #endregion

        #region Initialize
        /// <summary>
        /// 构造函数
        /// </summary>
        public CEmail()
        {
        }
        #endregion

        #region Methods
        /// <summary>
        /// Initialize the CSendEmail object first and set the correlative
        /// properties,then you can send mail
        /// </summary>
        /// <exception cref=" "></exception>
        public void SendMail()
        {
            errorMessage = "";
            System.Net.Mail.MailMessage aMessage;
            System.Net.Mail.SmtpClient Client = new SmtpClient();
            if (m_strSenderMail == "" || m_strSenderMail == null)
            {
                errorMessage = "Mail001";
                return;
            }
            if (m_strRecipientMail == "" || m_strRecipientMail == null)
            {
                errorMessage = "Mail002";
                return;
            }

            //Create a new message
            try
            {
                aMessage = new MailMessage(m_strSenderMail, m_strRecipientMail);
                if (m_strMailSubject != null) aMessage.Subject = m_strMailSubject;
                if (m_encodingMailBodyEncoding != null) aMessage.SubjectEncoding = m_encodingMailBodyEncoding;
                if (m_encodingMailBodyEncoding != null) aMessage.BodyEncoding = m_encodingMailBodyEncoding;


                aMessage.IsBodyHtml = true;

                if (m_strMailBody != null) aMessage.Body = m_strMailBody;

                //add the attachment
                if (m_strarrayMailAttachmentList != null)
                {
                    aMessage.Attachments.Add(new System.Net.Mail.Attachment(m_strarrayMailAttachmentList));
                }

                //create a smtpMail


                Client.Host = MailServerName;

                if (MailAuthentication) //如果使用身份验证
                {
                    Client.UseDefaultCredentials = false;
                    Client.Credentials = new System.Net.NetworkCredential(m_lMailUsername, m_lMailUserPassword);
                }
            }

            catch (Exception err)
            {
                errorMessage = err.Message;
                return;
            }

            // send email
            try
            {
                Client.Send(aMessage);

            }
            catch (Exception err)
            {
                errorMessage = err.Message;
            }


        }
        #endregion

    }

}


